<?php

namespace App\Http\Controllers;

use App\Common\Constant\Status;
use App\Notifications\OrderNotification;
use App\Notifications\SystemNotification;
use App\Services\IMasterService;
use Illuminate\Http\Request;
use Illuminate\Notifications\DatabaseNotification;

/**
 * 消息
 * Class MessageController
 * @package App\Http\Controllers
 */
class MessageController extends BaseController
{
    /**
     * 首页
     * @param Request $request
     */
    public function index(Request $request,IMasterService $masterService){
        list($system_message_count,$order_message_count) = $masterService->getMessageCount(getCurrentUserid());
        return view('message/index',['system_message_count'=>$system_message_count,'order_message_count'=>$order_message_count]);
    }

    /**
     * 系统消息
     * @param Request $request
     */
    public function listSystem(Request $request,IMasterService $masterService){
        $listSystemMessage = $masterService->listSystemMessage(getCurrentUserid());
        return view('message/listSystem',['listSystemMessage'=>$listSystemMessage]);
    }

    /**
     * 订单消息
     * @param Request $request
     */
    public function listOrder(Request $request,IMasterService $masterService){
        $listOrderMessage = $masterService->listOrderMessage(getCurrentUserid());
        return view('message/listOrder',['listOrderMessage'=>$listOrderMessage]);
    }

    /**
     * 读消息
     * @param $notification_id
     * @param Request $request
     */
    public function read(DatabaseNotification $notification,Request $request)
    {
        //置为已读
        if ($notification->unread()){
            $notification->markAsRead();
        }

        switch (true){
            case starts_with($notification->type,SystemNotification::class)://系统消息
                return $notification->data['message'];
            case starts_with($notification->type,OrderNotification::class)://订单消息
                return redirect(url("order/detail/{$notification->data['order_id']}"));
        }
        return $this->renderFail(Status::MESSAGE_UNKNOWN_TYPE);
    }

}
